{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.keras.layers import Softmax\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Softmax层\n",
    "\n",
    "[softmax](https://windmissing.github.io/Bible-DeepLearning/Chapter6/2Gradient/2OutputUnit/3Softmax.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(5,), dtype=float32, numpy=\n",
       "array([0.01165623, 0.03168492, 0.08612854, 0.23412165, 0.6364086 ],\n",
       "      dtype=float32)>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = tf.constant([1,2,3,4,5],dtype=tf.float32)\n",
    "layer = Softmax()\n",
    "layer(X)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 对二维数据的某一个维度做softmax\n",
    "\n",
    "输入二维数据为：  \n",
    "1 2 3  \n",
    "1 1 1  \n",
    "默认对最低维度做softmax,即分别对[1 2 3]和[1 1 1]做softmax"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(2, 3), dtype=float32, numpy=\n",
       "array([[0.09003057, 0.24472848, 0.66524094],\n",
       "       [0.33333334, 0.33333334, 0.33333334]], dtype=float32)>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = tf.constant([[1,2,3],[1,1,1]],dtype=tf.float32)\n",
    "layer = Softmax()\n",
    "layer(X)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "也可以指定对哪个维度做softmax,例如  \n",
    "输入二维数据  \n",
    "1 2 3  \n",
    "1 1 1  \n",
    "指定axis=0，分别对[1 1]、[2 1]、[3 1]做softmax"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(2, 3), dtype=float32, numpy=\n",
       "array([[0.5       , 0.7310586 , 0.880797  ],\n",
       "       [0.5       , 0.26894143, 0.11920291]], dtype=float32)>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = tf.constant([[1,2,3],[1,1,1]],dtype=tf.float32)\n",
    "layer = Softmax(axis=0)\n",
    "layer(X)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "以上方法对高维数据同样适用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
